Notebook 4 Applying a classifier to a folder of h5 files

In this notebook we go through using a trained classifier to detect activity on new data.

In [1]:
import sys
import os

Loading the pyecog module

The easiest place in which to download and run this notebook is the pyecog directory downloaded from github, e.g. “pyecog-Development” as the pyecog module will be found in this folder. However, if you want to run the notebook from elsewhere on your computer you first need to make sure that python can find the pyecog module using sys.path.append(). To do this modify and copy the following code into a cell and run it (shift+enter).

pyecog_path = '/home/jonathan/git_repos/pyecog' # replace this with the pyecog's location on your computer
sys.path.append(pyecog_path)

If you are on windows you have to deal with the problem that backslashes in your paths (when you copy them) are treated escape characters by python. Prefixing the string with ‘r’ prevents this, as python creates a “raw” string literal in which  are treated as literal characters, not escape characters.

pyecog_path = r'C:\home\jonathan\git_repos\pyecog'  # replace this with the pyecog's location on your computer
In [2]:
# note if you are in the directory downloaded from github you do not have to run this cell
#pyecog_path = r'C:\Users\Eleonora Lugara\Desktop\pyecog-Development'
pyecog_path = '/home/jonathan/git_repos/pyecog'
sys.path.append(pyecog_path)
In [3]:
import pyecog as pg
pg
Out[3]:
<module 'pyecog' from '/home/jonathan/git_repos/pyecog/pyecog/__init__.py'>

Warnings and errors!

  1. If you have an error message like:
ImportError: No module named 'pyecog'
This probably means you haven't set he "pyecog_path" variable correctly
  1. If you have a different warning or error. Check you have activated your pyecog environment before running jupyter notebook. On a windows machine, in anaconda prompt:
>> activate pyecog_env # "source activate pyecog" for mac or linux
>> jupyter notebook

Set up path variables:

We just need to tell python where to find the classifier and which directory to apply in.

In [4]:
clf_filepath   = '/media/jonathan/DATA/seizure_data/tawfeeq/clf_tawfeeq_library_pandasversion_19_2.p'
h5_folderpath  = '/media/jonathan/My Passport-ELE -EEG/2019/analysis pyecog/01_baselineconverted_01'

# this is the scond load 2019/09/20
h5_folderpath  = '/media/jonathan/My Passport-ELE -EEG/2019/analysis pyecog/01_baselineconverted_02'
In [5]:
# load the classifier
clf = pg.classifier.load_classifier(clf_filepath)
/home/jonathan/anaconda3/envs/pyecog_env/lib/python3.5/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator DecisionTreeClassifier from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
/home/jonathan/anaconda3/envs/pyecog_env/lib/python3.5/site-packages/sklearn/base.py:311: UserWarning: Trying to unpickle estimator RandomForestClassifier from version 0.18.1 when using version 0.19.1. This might lead to breaking code or invalid results. Use at your own risk.
  UserWarning)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-1395c6bfc7ec> in <module>()
      1 # load the classifier
----> 2 clf = pg.classifier.load_classifier(clf_filepath)

~/git_repos/pyecog/pyecog/ndf/classifier.py in load_classifier(filepath)
    211 def load_classifier(filepath):
    212     f = open(filepath, 'rb')
--> 213     clf = pickle.load(f)
    214     return clf
    215

ImportError: No module named 'pandas.indexes'

Use classifier to predict seizures

We then pass the “predict_dir” method two arguments. The name of the predictions file we want to make, and the folder path that we want to classify. By default, after the classifier has detected 50 seizures, the predictions csv file will be saved, and you can load up the predictions in the gui.

In [6]:
predictions_csv = '/media/jonathan/My Passport-ELE -EEG/2019/analysis pyecog/prediction from Tawfeeq Library/predictions_baselineconverted02.csv'
In [7]:
clf.predict_directory(h5_folderpath, output_csv_filename=predictions_csv)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-d531254a052b> in <module>()
----> 1 clf.predict_directory(h5_folderpath, output_csv_filename=predictions_csv)

NameError: name 'clf' is not defined
In [8]: